package org.book.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.book.dao.BookBean;
import org.book.service.BookService;

/**
 * Servlet implementation class BookUpdateServlet
 */
@WebServlet("/BookUpdateServlet")
public class BookUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		BookService bookService = new BookService();
		
		String pageCurrent2 = request.getParameter("pageCurrent2");
		
		String no = request.getParameter("no");
		String ISBN = request.getParameter("ISBN");
		String bookName = request.getParameter("bookName");
		String price = request.getParameter("price");
		String press = request.getParameter("press");
		String author = request.getParameter("author");
		
		if(no!=null&&ISBN!=null&&bookName!=null&&price!=null&&press!=null&&author!=null){
			
			int id = Integer.parseInt(no);
			BookBean stu = new BookBean(ISBN,bookName,Double.parseDouble(price),press,author);
			//添加
			if(id==-1) {
				
				boolean bool = bookService.add(stu);
				
				if(bool) {
					System.out.println("添加成功");
					response.sendRedirect("BooksServlet");
					//request.getRequestDispatcher("BooksServlet").forward(request, response);
				}else {
					System.out.println("添加失败");
					request.getRequestDispatcher("BooksServlet").forward(request, response);
				}
			}
			//修改
			else {
				
				boolean bool = bookService.update(id, stu);
				
				if(bool) {
					System.out.println("修改成功");
					request.setAttribute("pageCurrent2", pageCurrent2);
					request.getRequestDispatcher("BooksServlet").forward(request, response);
				}else {
					System.out.println("修改失败");
					request.getRequestDispatcher("BooksServlet").forward(request, response);
				}
			}
			
			
			
		}
		//删除
		else if(no!=null&&ISBN==null){
			int id = Integer.parseInt(no);
			boolean bool = bookService.remove(id);
			if(bool) {
				System.out.println("删除成功");
				request.setAttribute("pageCurrent2", pageCurrent2);
				request.getRequestDispatcher("BooksServlet").forward(request, response);
			}else {
				System.out.println("删除失败");
				request.getRequestDispatcher("BooksServlet").forward(request, response);
			}
		}
		//错误
		else {
			System.out.println("输入错误");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
